package com.ztom.top100;

/**
 * 最小栈
 * <p>
 * https://leetcode-cn.com/problems/min-stack/
 *
 * @author ZhangTao
 */
public class Code55MinStack {

    class MinStack {

        /**
         * 永远指向栈顶, 之前的元素挂在 next
         */
        private Node head;

        public MinStack() {

        }

        public void push(int val) {
            if (head == null) {
                // 空栈
                head = new Node(val, val, null);
            } else {
                // 栈不为空
                head = new Node(val, Math.min(val, head.min), head);
            }
        }

        public void pop() {
           head = head.next;
        }

        public int top() {
            return head.v;
        }

        public int getMin() {
            return head.min;
        }

        private class Node {
            private int v;
            private int min;
            private Node next;

            public Node(int v, int min, Node next) {
                this.v = v;
                this.min = min;
                this.next = next;
            }
        }
    }
}
